package com.hcj.testThreadPool;

import org.eclipse.jetty.util.thread.ExecutorThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * @Author: HuangChangjin 2017-12-05 19:07
 * @Description:
 */
public class ExecutorProcessPool {
    private static final Logger log = LoggerFactory.getLogger(ExecutorThreadPool.class);
    private ExecutorService executorService;
    private static ExecutorProcessPool pool = new ExecutorProcessPool();
    private final int threadMax = 10;
    public ExecutorProcessPool(){
        log.info("开始处理线程》》》》threadMax={}", threadMax);
//        executorService = ExecutorServiceFactory.getInstance().createFixedThreadPool(threadMax);
        executorService = ExecutorServiceFactory.getInstance().createCachedThreadPool();
    }

    public static ExecutorProcessPool getInstance(){
        return pool;
    }

    /** 关闭线程池：执行完队列中的所有任务后，才关闭 */
    public void shutdown(){
        executorService.shutdown();
    }

    /** 提交任务到线程池，可以接收线程返回值 */
    public Future<?> submit(Runnable task){
        return executorService.submit(task);
    }

    /** 提交任务到线程池，可以接收线程返回值 */
    public Future<?> submit(Callable<?> task){
        return executorService.submit(task);
    }

    /** 提交任务到线程池 */
    public void execute(Runnable task){
        executorService.execute(task);
    }

}
